home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / phillips / bilinear.c < prev    next >
Encoding:
Text File  |  1994-02-26  |  2.0 KB  |  78 lines

  1.  
  2.      /*******************************************
  3.      *
  4.      *   bilinear_interpolate(..
  5.      *
  6.      *   This routine performs bi-linear
  7.      *   interpolation.
  8.      *
  9.      *   If x or y is out of range, i.e. less
  10.      *   than zero or greater than ROWS or COLS,
  11.      *   this routine returns a zero.
  12.      *
  13.      *   If x and y are both in range, this
  14.      *   routine interpolates in the horizontal
  15.      *   and vertical directions and returns
  16.      *   the proper gray level.
  17.      *
  18.      *******************************************/
  19.  
  20. bilinear_interpolate(the_image, x, y)
  21.    double x, y;
  22.    short  the_image[ROWS][COLS];
  23. {
  24.    double fraction_x, fraction_y,
  25.           one_minus_x, one_minus_y,
  26.           tmp_double;
  27.    int    ceil_x, ceil_y, floor_x, floor_y;
  28.    short  p1, p2, p3, result = FILL;
  29.  
  30.       /******************************
  31.       *
  32.       *   If x or y is out of range,
  33.       *   return a FILL.
  34.       *
  35.       *******************************/
  36.  
  37.    if(x < 0.0               ||
  38.       x >= (double)(COLS)   ||
  39.       y < 0.0               ||
  40.       y >= (double)(ROWS))
  41.       return(result);
  42.  
  43.    tmp_double = floor(x);
  44.    floor_x    = tmp_double;
  45.    tmp_double = floor(y);
  46.    floor_y    = tmp_double;
  47.    tmp_double = ceil(x);
  48.    ceil_x     = tmp_double;
  49.    tmp_double = ceil(y);
  50.    ceil_y     = tmp_double;
  51.  
  52.    fraction_x = x - floor(x);
  53.    fraction_y = y - floor(y);
  54.  
  55.    one_minus_x = 1.0 - fraction_x;
  56.    one_minus_y = 1.0 - fraction_y;
  57.  
  58.    tmp_double = one_minus_x * 
  59.                 (double)(the_image[floor_y][floor_x]) +
  60.                 fraction_x * 
  61.                 (double)(the_image[floor_y][ceil_x]);
  62.    p1         = tmp_double;
  63.  
  64.    tmp_double = one_minus_x * 
  65.                 (double)(the_image[ceil_y][floor_x]) +
  66.                 fraction_x * 
  67.                 (double)(the_image[ceil_y][ceil_x]);
  68.    p2         = tmp_double;
  69.  
  70.    tmp_double = one_minus_y * (double)(p1) +
  71.                 fraction_y * (double)(p2);
  72.    p3         = tmp_double;
  73.  
  74.  
  75.    return(p3);
  76.  
  77. }  /* ends bilinear_interpolate */
  78.